package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import domain.Contact;
import domain.ContactGroup;
import domain.Entreprise;

public class DAOContact extends HibernateDaoSupport {

	public void addContact(Contact c) {
		this.getHibernateTemplate().saveOrUpdate(c);
	}

	public void delete(int id) {

		HibernateTemplate hiberTempl = this.getHibernateTemplate();
		Contact theContact = (Contact) hiberTempl.load(Contact.class, id);
		if (theContact != null)
			hiberTempl.delete(theContact);
	}

	@SuppressWarnings("unchecked")
	public List<Contact> searchByName(String name) {

		DetachedCriteria criterion = DetachedCriteria.forClass(Contact.class)
				.add(Restrictions.like("firstName", "%" + name + "%"));
		return (List<Contact>) this.getHibernateTemplate().findByCriteria(
				criterion);

	}

	@SuppressWarnings("unchecked")
	public List<Contact> searchByEmail(String email) {

		DetachedCriteria criterion = DetachedCriteria.forClass(Contact.class)
				.add(Restrictions.like("email", "%" + email + "%"));
		return (List<Contact>) this.getHibernateTemplate().findByCriteria(
				criterion);

	}

	public Contact searchById(int id) {

		@SuppressWarnings("unchecked")
		List<Contact> contacts = (List<Contact>) this.getHibernateTemplate()
				.findByNamedParam("from Contact c where c.idContact= :id",
						"id", id);
		if (contacts != null && contacts.size() > 0)
			return contacts.get(0);
		return null;
	}

	public void updateContact(Contact contact) {
		this.getHibernateTemplate().saveOrUpdate(contact);
	}

	public void updateEnterprise(Entreprise ent) {
		this.getHibernateTemplate().saveOrUpdate(ent);
	}

	/**
	 * returns all stored contacts
	 */
	public List<Contact> search() {
		@SuppressWarnings("unchecked")
		List<Contact> contacts = (List<Contact>) this.getHibernateTemplate()
				.find("from Contact");
		ArrayList<Contact> results = new ArrayList<Contact>();
		for (Contact contact : contacts) {
			if (!(contact instanceof Entreprise))
				results.add(contact);
		}
		return results;
	}

	public List<Contact> searchByGroupID(String idGrp) {
		@SuppressWarnings("unchecked")
		List<Contact> contacts = (List<Contact>) this
				.getHibernateTemplate()
				.findByNamedParam(
						"select distinct c from Contact c inner join c.books g where g.groupName = :id",
						"id", idGrp);
		return contacts;
	}

	/**
	 * returns all stored contacts
	 */
	public List<Entreprise> searchEnt() {
		@SuppressWarnings("unchecked")
		List<Contact> contacts = (List<Contact>) this.getHibernateTemplate()
				.find("from Contact");
		ArrayList<Entreprise> results = new ArrayList<Entreprise>();
		for (Contact contact : contacts) {
			if (contact instanceof Entreprise)
				results.add((Entreprise) contact);
		}
		return results;
	}

	public ContactGroup find(String name) {
		if (name == null)
			return null;
		HibernateTemplate templ = this.getHibernateTemplate();
		@SuppressWarnings("unchecked")
		List<ContactGroup> groups = (List<ContactGroup>) templ
				.findByNamedParam(
						"from ContactGroup c where c.groupName = :name",
						"name", name);
		if (groups != null && groups.size() > 0)
			return groups.get(0);
		return null;
	}
}
